package com.atguigu.tingshu.album.service.impl;

import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.model.album.*;
import com.atguigu.tingshu.vo.category.CategoryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

	@Autowired
	private BaseCategory1Mapper baseCategory1Mapper;

	@Autowired
	private BaseCategory2Mapper baseCategory2Mapper;

	@Autowired
	private BaseCategory3Mapper baseCategory3Mapper;

	@Autowired
	private BaseCategoryViewMapper baseCategoryViewMapper;

	@Autowired
	private BaseAttributeMapper baseAttributeMapper;

    @Override
    public List<CategoryVo> getBaseCategoryList() {
		return baseCategoryViewMapper.getBaseCategoryList();
    }

    @Override
    public List<BaseAttribute> findAttributeAndValue(Long c1Id) {
		return  baseAttributeMapper.findAttributeAndValue(c1Id);
    }

    @Override
    public List<BaseCategory3> getC1Id7TopBaseCategory3List(Long c1Id) {
		// 1.去二级分类对象表中根据一级分类id查询 所有的二级分类对象
		LambdaQueryWrapper<BaseCategory2> wrapper = new LambdaQueryWrapper<BaseCategory2>();
		wrapper.eq(BaseCategory2::getCategory1Id, c1Id);
		wrapper.eq(BaseCategory2::getIsDeleted, 0);
		List<BaseCategory2> baseCategory2s = baseCategory2Mapper.selectList(wrapper);
		if (CollectionUtils.isEmpty(baseCategory2s)) {
			throw new GuiguException(201, "该一级分类下不存在任何的二级分类集合");
		}

		List<Long> c2Ids = baseCategory2s.stream().map(BaseCategory2::getId).collect(Collectors.toList());
		LambdaQueryWrapper<BaseCategory3> wrapper1 = new LambdaQueryWrapper<BaseCategory3>();
		wrapper1.in(BaseCategory3::getCategory2Id, c2Ids);
		wrapper1.eq(BaseCategory3::getIsTop, 1);
		wrapper1.last("limit 7");
		wrapper1.eq(BaseCategory3::getIsDeleted, 0);


		List<BaseCategory3> baseCategory3s = baseCategory3Mapper.selectList(wrapper1);
		if (CollectionUtils.isEmpty(baseCategory3s)) {
			return new ArrayList<>();
		}
		return baseCategory3s;
    }

	/**
	 * 获取所有基础分类列表
	 * 根据专辑ID查询并构建包含所有子分类的分类视图对象
	 *
	 * @param albumId 专辑ID，用于查询一级分类
	 * @return CategoryVo 包含所有子分类的分类视图对象
	 */
	@Override
	public CategoryVo getAllBaseCategoryList(Long albumId) {
		// 创建一个 CategoryVo 对象，用于存储最终的分类信息
		CategoryVo categoryVo = new CategoryVo();
		// 查询与 albumId 对应的一级分类
		BaseCategory1 baseCategory1 = baseCategory1Mapper.selectById(albumId);
		// 如果查询到一级分类，则设置其 ID 和名称到 categoryVo 中
		if (baseCategory1 != null) {
			categoryVo.setCategoryId(baseCategory1.getId());
			categoryVo.setCategoryName(baseCategory1.getName());
			categoryVo.setCategoryChild(new ArrayList<>());
		} else {
			throw new RuntimeException("该专辑不存在一级分类");
		}

		// 查询所有与一级分类 ID (albumId) 相关的二级分类
		List<BaseCategory2> baseCategory2s = baseCategory2Mapper.selectList(
				new LambdaQueryWrapper<BaseCategory2>()
						.eq(BaseCategory2::getCategory1Id, albumId));
		// 如果存在二级分类，则遍历每个二级分类
		System.out.println("baseCategory2s = " + baseCategory2s);
		if (!CollectionUtils.isEmpty(baseCategory2s)) {

			baseCategory2s.forEach(baseCategory2 -> {
				// 创建一个新的 CategoryVo 对象来存储当前二级分类的信息
				CategoryVo categoryVo2 = new CategoryVo();
				categoryVo2.setCategoryId(baseCategory2.getId());
				categoryVo2.setCategoryName(baseCategory2.getName());
				// 将二级分类信息添加到 categoryVo 的子分类列表中
				categoryVo.getCategoryChild().add(categoryVo2);

				// 查询所有与当前二级分类 ID 相关的三级分类
				List<BaseCategory3> baseCategory3s = baseCategory3Mapper.selectList(
						new LambdaQueryWrapper<BaseCategory3>()
								.eq(BaseCategory3::getCategory2Id, baseCategory2.getId()));
				// 如果存在三级分类，则遍历每个三级分类
				categoryVo2.setCategoryChild(new ArrayList<>());
				if (!CollectionUtils.isEmpty(baseCategory3s)) {
					baseCategory3s.forEach(baseCategory3 -> {
						// 创建一个新的 CategoryVo 对象来存储当前三级分类的信息
						CategoryVo categoryVo3 = new CategoryVo();
						categoryVo3.setCategoryId(baseCategory3.getId());
						categoryVo3.setCategoryName(baseCategory3.getName());
						// 将三级分类信息添加到当前二级分类的子分类列表中
						categoryVo2.getCategoryChild().add(categoryVo3);
					});
				} else {
					throw new RuntimeException("该专辑不存在三级分类");
				}
			});
		} else {
			throw new RuntimeException("该专辑不存在二级分类");
		}
		// 返回包含所有分类信息的 categoryVo 对象
		return categoryVo;
	}

	@Override
	public List<Long> getCategory1IdList() {
		List<BaseCategory1> baseCategory1s = baseCategory1Mapper.selectList(null);
		return baseCategory1s.stream().map(BaseCategory1::getId).collect(Collectors.toList());
	}

}
